/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAOpeningHoursTopCell.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 06/11/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAOpeningHoursTopCell.h"
#import <MapKit/MapKit.h>

@interface BAOpeningHoursTopCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *openCloseLabel;
@property (weak, nonatomic) IBOutlet UIView *openCloseDot;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;

@end

@implementation BAOpeningHoursTopCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)awakeFromNib{
    BA_LABEL_SET_FONT_REGULAR(self.nameLabel);
    BA_LABEL_SET_FONT_LIGHT(self.addressLabel);
    BA_LABEL_SET_FONT_LIGHT(self.emailLabel);
    BA_LABEL_SET_FONT_LIGHT(self.openCloseLabel);
    self.openCloseDot.layer.cornerRadius = 5;
}

-(void)setLibrary:(BAOpeningHoursLibrary *)library{
    _library = library;
    [self.nameLabel setText:library.name];
    
    NSString* phonefield = @"";
    if ([library.phone length]>0) {
        phonefield = [NSString stringWithFormat:@"Tlf: %@",library.phone];
    }
    
    [self.addressLabel setText:[NSString stringWithFormat:@"%@\n%@ %@\n%@",
                                library.street,
                                library.postcode,
                                library.city,
                                phonefield]];
    
    NSString* emailfield = @"";
    if ([library.mail length]>0) {
        emailfield = [NSString stringWithFormat:@"Email: %@",library.mail];
    }
    self.emailLabel.text = emailfield;
    
    UIColor* greenColor = [UIColor colorWithRed:91.0/255.0 green:218.0/255.0 blue:112.0/255.0 alpha:1];
    
    self.openCloseDot.backgroundColor = library.isOpen ? greenColor : [UIColor redColor];
    self.openCloseLabel.text = library.isOpen ? @"Åben" :  @"Lukket";
    
}

- (IBAction)directionsButtonClicked:(id)sender {
    NSString* address = [NSString stringWithFormat:@"%@,%@ %@", self.library.street, self.library.postcode, self.library.city];
    NSString* adress_url = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@",[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:adress_url]];
}

@end
